﻿using FTBS.Models.BLO;
using FTBS.Models.Entities;
using FTBS.Models.Entities.Concert;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;

namespace FTBS.Controllers
{
    public class ShowTimeController : Controller
    {
        private ShowTimeBLO _showTimeBLO = new ShowTimeBLO();
        private TheaterBLO _theaterBLO = new TheaterBLO();
        //private SettingTheaterBLO _settingTheaterBlo = new SettingTheaterBLO();

        /// <summary>
        /// Load all showtime into table 
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page, bool? isSortDescending)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return Redirect("/Home/Announcement");
            }
            List<ShowTimeConcertEntity> listShowTime = new List<ShowTimeConcertEntity>();
            listShowTime = _showTimeBLO.GetAllShowTime();

            //ViewBag.Theater = new SelectList(_theaterBLO.GetAllTheaterInfo(), "TheaterId", "TheaterName");
            ViewBag.NameSortParm = "date_desc";
            ViewBag.CurrentSort = sortOrder;

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;
            ViewBag.IsSortDes = false;

            if (!String.IsNullOrEmpty(searchString))
            {
                listShowTime = listShowTime.Where(showtime => showtime.TheaterName.ToUpper().Contains(searchString.ToUpper())
                                                     || showtime.Time.ToString().ToUpper().Contains(searchString.ToUpper())).ToList();
            }

            switch (sortOrder)
            {
                case "time_desc":
                    if (isSortDescending == true)
                    {
                        listShowTime = listShowTime.OrderByDescending(showtime => showtime.Time).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        listShowTime = listShowTime.OrderBy(showtime => showtime.Time).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "theater_desc":
                    if (isSortDescending == true)
                    {
                        listShowTime = listShowTime.OrderByDescending(showtime => showtime.TheaterName).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        listShowTime = listShowTime.OrderBy(showtime => showtime.TheaterName).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                default:
                    listShowTime = listShowTime.OrderBy(showtime => showtime.TheaterId).ToList();
                    break;
            }

            int pageSize = 5;
            int pageNumber = (page ?? 1);

            return View(listShowTime.ToPagedList(pageNumber, pageSize));
        }

        /// <summary>
        /// Load page create
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <author>VuongVT</author>
        /// <date>3/8/2014</date>
        public ActionResult CreateShowTime()
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return Redirect("/Home/Announcement");
            }
            ViewBag.CurrentDay = DateTime.Now.Date;
            ViewBag.Theater = new SelectList(_theaterBLO.GetAllTheaterInfo(), "TheaterId", "TheaterName");
            ViewBag.IsExist = false;
            return View();
        }

        /// <summary>
        /// Create a schedule film
        /// </summary>
        /// <param name="format"></param>
        /// <returns></returns>
        /// <author>vuongvt</author>
        /// <date>3/3/2014</date>
        [HttpPost]
        public ActionResult CreateShowTime(ShowTime showtime)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return Redirect("/Home/Announcement");
            }
            if (ModelState.IsValid)
            {
                //Check is ShowTime existed or not
                if (!_showTimeBLO.IsExistShowTimeCreate(showtime))
                {
                    ViewBag.IsExist = false;
                    _showTimeBLO.CreateShowTime(showtime);
                    return RedirectToAction("Index");
                }
                else
                {
                    ViewBag.Theater = new SelectList(_theaterBLO.GetAllTheaterInfo(), "TheaterId", "TheaterName", showtime.TheaterId);
                    ViewBag.IsExist = true;
                    return View(showtime);
                }
            }
            else
            {
                ViewBag.Theater = new SelectList(_theaterBLO.GetAllTheaterInfo(), "TheaterId", "TheaterName", showtime.TheaterId);
                ViewBag.IsExist = false;
                return View(showtime);
            }
        }

        /// <summary>
        /// Load object ShowTime by ShowTimeId
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <author>VuongVT</author>
        /// <date>3/8/2014</date>
        public ActionResult EditShowTime(int id = 0)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return Redirect("/Home/Announcement");
            }
            //If ShowTime is using and have customer/guest booked ticket -> Don't Edit
            if (_showTimeBLO.IsUserInScheduleAndBooked(id))
            {
                TempData["IsUsedInSchedule"] = true;
                return RedirectToAction("Index");
            }
            else
            {
                ShowTime showTime = _showTimeBLO.GetShowTimeById(id);
                ViewBag.IsValidateTime = 1; //1: Validate Time
                ViewBag.IsExist = false;
                if (showTime == null)
                {
                    return HttpNotFound();
                }

                ViewBag.Theater = new SelectList(_theaterBLO.GetAllTheaterInfo(), "TheaterId", "TheaterName", showTime.TheaterId);
                return View(showTime);
            }
        }

        /// <summary>
        /// Edit ShowTime Information
        /// </summary>
        /// <param name="format"></param>
        /// <returns></returns>
        /// <author>VuongVT</author>
        /// <date>3/8/2014</date>
        [HttpPost]
        public ActionResult EditShowTime(ShowTime showTime)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return Redirect("/Home/Announcement");
            }
            if (ModelState.IsValid)
            {
                if (!_showTimeBLO.IsExistShowTimeUpdate(showTime))
                {
                    ViewBag.IsExist = false;
                    _showTimeBLO.EditShowTime(showTime);
                    return RedirectToAction("Index");
                }
                else
                {
                    ViewBag.Theater = new SelectList(_theaterBLO.GetAllTheaterInfo(), "TheaterId", "TheaterName", showTime.TheaterId);
                    ViewBag.IsExist = true;
                    return View(showTime);
                }
            }
            else
            {
                ViewBag.Theater = new SelectList(_theaterBLO.GetAllTheaterInfo(), "TheaterId", "TheaterName", showTime.TheaterId);
                ViewBag.IsExist = false;
                return View(showTime);
            }
        }

        /// <summary>
        /// Delete a showtime
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <author>hungph</author>
        /// <date>3/3/2014</date>
        [HttpDelete, ActionName("DeleteShowTime")]
        public ActionResult DeleteShowTime(int id = 0)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return Redirect("/Home/Announcement");
            }
            if (_showTimeBLO.IsUsedForSchedule(id))
            {
                TempData["CanNotDelete"] = true;
                return RedirectToAction("Index");
            }
            else
            {
                ShowTime showtime = _showTimeBLO.GetShowTimeById(id);
                if (showtime == null)
                {
                    return HttpNotFound();
                }
                _showTimeBLO.DeleteShowTime(showtime);
                return RedirectToAction("Index");
            }
        }
    }
}
